<?php

class Controller_Admin_Users extends Controller_Admin_Template {
	
	public function before() {
		parent::before();
		
		$this->template->title = __('Users');
	}
	public function action_index() {
		$admin_users = ORM::factory('role', 2)
			->users
			->order_by('last_name')
			->find_all();
		$login_users = ORM::factory('role', 1)
			->users
			->order_by('last_name')
			->find_all();
		
		$view = View::factory('admin/users/index');
		$view->set('admin_users', $admin_users);
		$view->set('login_users', $login_users);
		$this->template->title = __('Users');
		$this->template->content = $view;
	}
	
	public function action_view() {
		$user_id = $this->request->param('id');
		$user = ORM::factory('user', $user_id);
		$view = View::factory('admin/users/view');		
		$view->set('user', $user);
		$this->template->content = $view;
	}
	
	public function action_orders() {
		$user_id = $this->request->param('id');
		$user = ORM::factory('user', $user_id);
		$orders = $user
			->orders
			->order_by('date_placed', 'desc')
			->find_all();
		$view = View::factory('admin/users/orders');		
		$view->set('user', $user);
		$view->set('orders', $orders);
		$this->template->content = $view;
	}
	
	public function action_order() {
		$order_id = $this->request->param('id');
		$order = ORM::factory('order', $order_id);
		$view = View::factory('admin/users/order');
		$view->set('order', $order);
		
		$this->template->content = $view;
	}
	
	public function action_delete() {
		$user_id = $this->request->param('id');
		$user = ORM::factory('user', $user_id);
		$user->delete();	
		$this->request->redirect('admin/users');
	}
}